import { getPlay, productstatistics } from '../../../api/getData.js'
import { formatTime } from '../../../utils/util.js'
Component({

  data: {
    date: '',
    play: null,
    product: null
  },
  attached() {
    let date = formatTime(new Date(), 1, true)
    this.setData({ date })
    this.getDayPlay(date)
  },
  pageLifetimes: {
    
  },
  methods: {
    bindDateChange(e) {
      let value = e.detail.value;
      this.setData({
        date: value
      })
      this.getDayPlay(value)
    },

    // 修改天计划
    dayPlayInput(e){
      let value = e.detail.value;
      this.getProductstatistics({
        dayPlay: value,
        useDayPlay: true
      })
    },

    // 修改小时计划
    hourPlayInput(e){
      let value = e.detail.value;
      let index = e.currentTarget.dataset.index;
      let { dailyPlan, todayDailyPlan} = this.data.product;
      dailyPlan[index] = value*1

      this.getProductstatistics({
        hourPlay: dailyPlan.join(','),
        useDayPlay: false
      })
    },

    // 前一天后一天
    changeDay(e){
      let today = this.data.date;
      let sign = e.currentTarget.dataset.sign;
      let stm = new Date(today.split('-').join('/')).getTime();
      let newDay = stm + sign * 86400000
      let date = formatTime(new Date(newDay), 1, true)
      this.setData({ date })
      this.getDayPlay(date)
    },

    // 获取天计划
    getDayPlay(date){
      getPlay(date)
        .then(res => {
          this.setData({
            play: res
          })
          this.getProductstatistics({ 
            dayPlay: res.dayPlay, 
            useDayPlay: res.useDayPlay,
            hourPlay: res.hourPlay
          })
        })
    },

    // 获取产量详情 data
    getProductstatistics(d){
      let { date } = this.data,
      { dayPlay } = d
      Object.assign(d, { date })
      productstatistics(d)
        .then(res => {
          if (res.resCode === "SUCCESS"){
            res.todayDailyPlan = dayPlay || res.todayDailyPlan;
            this.setData({
              product: res
            })
          }
         
        })
    }
  }
})